package com.fatri.lessionsimple

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.os.Environment
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.fatri.lessionsimple.camera.CameraAct
import com.permissionx.guolindev.PermissionX
import kotlinx.android.synthetic.main.act_home.*
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File

class HomeActivity : AppCompatActivity() {
    lateinit var map: HashMap<String, Class<out AppCompatActivity>>


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_home)

        map = HashMap()
        map.put("openGL", SimpleRenderActivity::class.java)
        map.put("openSL", AudioActivity::class.java)
        map.put("ffmpeg", MainActivity::class.java)
        map.put("camera", CameraAct::class.java)

        initView()
        checkPermission()
    }

    private fun initView() {
        map.keys.iterator().forEach {
            val clazz = map.get(it)

            val btn = Button(this)
            btn.text = it
            btn.isAllCaps = false
            btn.setOnClickListener {
                startActivity(Intent(this, clazz))
            }

            val params = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            ll_root.addView(btn, params)
        }
    }

    private fun checkPermission() {
        PermissionX.init(this)
            .permissions(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA
            )
            .request { allGranted, grantedList, deniedList ->
                if (allGranted) {

                } else {
                    Toast.makeText(this, "您拒绝了 权限", Toast.LENGTH_SHORT).show()
                }
            }
    }
}